Skip to content

fix(base): keep a text that fits exactly when float32 rounding falls short - #77

Closed
xiaoyumuxi wants to merge 1 commit into
mainfrom
fix/flex-rounding-shortfall
Closed

xiaoyumuxi wants to merge 1 commit into
mainfrom
fix/flex-rounding-shortfall

Conversation

@xiaoyumuxi

@xiaoyumuxi xiaoyumuxi commented Nov 28, 2026 •

Copy link
Copy Markdown

关联:上游 PR vivaui#507(同一改动,基于上游 main b036c88;本 PR 基于 fork main 1247fbd)

改了什么

一个容器按它自己测出来的宽度排版时,放得下的文字不再被截成省略号。

  • base/layout.go
    • solveFlow:剩余空间 free 为负,但不超过 float32 对容器尺寸的舍入误差(新函数 roundingSlack:每项按容器尺寸的 2 个 ulp 计)时,按 0 处理,不再从可收缩的子节点里扣。
    • 文字放得下的判断改成 n.Natural+n.PadX() <= n.Rect.Width,和测量首选宽度的加法一致;原来先减内边距再比,带内边距的文字会差一个 ulp。
  • base/flex_test.go:新增 TestARowAtItsMeasuredWidthKeepsAnEllipsizedLabelWhole,2000 种字宽 × 内边距 0 / 3,要求刚好放得下的“High”完整显示。

为什么

首选宽度和 solveFlow 分配空间时按不同顺序把同样的尺寸加了两遍,float32 下 free 会差几个 ulp 变成负数,被从可收缩的文字上扣掉,精确的放得下判断随即判定放不下,显示省略号。大约每 11 个字宽就有一个会被截。

Lithe 的 Agent 输入框底部,推理等级胶囊“High”因此显示成“Hi…”。修复前新测试输出:

flex_test.go:864: with a glyph 6.0350885 wide and padding 0, the label reads "…" in 24.14035, want High

怎么验证的

macOS 27.3(Apple Silicon)、go1.27.4:

  • gofmt -l .:没有输出
  • go vet ./...:没有 vet 报告(只有 cgo 的 Metal 弃用警告)
  • go test ./... -count=1(在上游分支上跑):除两类既有失败外全部通过,两类都在不带本改动的 b036c88 上一样出现:
  • 排版基准前后各 6 次取中位数:DeclaredPage 16847 → 16794 ns/op、Flow 48518 → 48202、StyledPage 63066 → 62920,都是 0 次分配;WrappedParagraphs 单独复测为 4956 → 4778,首轮的波动是噪声
  • Lithe 里临时把 Viva 指向本分支跑 TestVisualAgents(-tags 'vivahead lithevisual'):编辑区标签页的“High”胶囊完整显示,浅色、深色一致
  • 真实窗口:没有跑

边界在哪

框架缺陷修复,和具体应用无关。

Cost

没有。只在 free < 0 时多一次乘法和比较,没有新增分配。

…short

A row placed at the width it measured sums the same sizes again when it shares
them out, in another order, and float32 can leave it a few ulps short. solveFlow
took that shortfall from the children that shrink, so an ellipsized label that
fit exactly lost an ulp and was cut to an ellipsis (about one label width in
eleven). A shortfall within float32 rounding of the container's size is now no
shortfall, and a text's fit check sums its padding the way its preferred width
did.
@xiaoyumuxi

Copy link
Copy Markdown
Author

上游已以 vivaui#507(a3eda457)合入,并多处理了容器与排版方向垂直时的取整误差;#78 把它同步进 fork main(7a73f1ba),关闭。

@xiaoyumuxi xiaoyumuxi closed this Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant